package com.joy.common.interceptor;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.joy.common.model.vo.ResourceVO;
import com.joy.web.admin.entity.Resource;
import com.joy.web.admin.service.IResourceService;
import com.joy.web.admin.service.IUserService;
import com.joy.common.constant.CommonCons;
import com.joy.common.model.vo.UserInfoVO;
import lombok.extern.slf4j.Slf4j;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * @Description 页面初始化需要加载拦截器
 * @Author yjzhao
 * @Date 2019/9/17 16:32
 **/
@Slf4j
@Component
public class PageInitInterceptor extends HandlerInterceptorAdapter {

	@javax.annotation.Resource
	private IResourceService resourceService;

	@javax.annotation.Resource
	private IUserService userService;

	@Override
	public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response,
                           Object handler, ModelAndView mav) throws Exception {
		if (mav == null) {
			return;
		}
		//1. 登录用户获取 页面左栏 menu 列表 Start
		Subject subject = SecurityUtils.getSubject();
		if (subject == null || subject.getPrincipal() == null) {
			return;
		}
		String userNm = (String)subject.getPrincipal();
		if (StrUtil.isEmpty(userNm)) {
			return;
		}
		//向request 传拥有权限 menu
		try {
			List<ResourceVO> menuTree = resourceService.getMenuTreeByUserNm(userNm);
			mav.addObject("menuList", menuTree);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//用户信息
		try {
			UserInfoVO userInfoVO = userService.findUserInfoByUserNm(userNm);;
			mav.addObject("userInfoVO", userInfoVO);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//页面标签信息
		try {

			RequestMapping annotation = null;
			if (handler instanceof HandlerMethod) {
				HandlerMethod h = (HandlerMethod) handler;
				annotation = h.getMethod().getDeclaringClass().getAnnotation(RequestMapping.class);
				String[] controllerUri = annotation.value();

				if (controllerUri != null && controllerUri.length > 0) {
					Cache actionLikeKeyCache = CacheManager.getInstance().getCache(CommonCons.MyCache.ACTION_LIKE_KEYS);
					Element actionLikeKeyElement = actionLikeKeyCache.get(controllerUri[0]);
					if (actionLikeKeyElement != null) {
						Map<String, Object> actionLikeKey = (Map<String, Object>) actionLikeKeyElement.getObjectValue();

						String code = "";
						LinkedList<Resource> link = Lists.newLinkedList();
						code = (String) actionLikeKey.get("code");
						link = (LinkedList<Resource>) actionLikeKey.get("links");
						mav.addObject("_url_code", code)
								.addObject("_url_link", link)
								.addObject("_resource_name", link.size() == 0 ? "" : link.getLast().getResourceName());
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {



		return super.preHandle(request, response, handler);
	}
}
